package bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.event.SelectEvent;

import dao.DoctorDAO;
import dao.DoctorSpecialtyDAO;
import dao.JPA;
import entity.Doctor;

@ManagedBean
@ViewScoped
public class FindDoctorBean implements Serializable {
	
	@Inject @JPA
    private DoctorDAO doctorDAO;	
	@Inject @JPA
    private DoctorSpecialtyDAO doctorSpecialtyDAO;	
	private List<Doctor> resultList;
	private List<String> doctorSpecialtyList = new ArrayList<>();
	private String selectedSpec;
	
	@PostConstruct
	private void init() {
		resultList = doctorDAO.findAll();	
		doctorSpecialtyList.add("All");
		doctorSpecialtyList.addAll(doctorSpecialtyDAO.selectDoctorSpecialtyNames());
	}
	
	public void onRowSelect(SelectEvent event) {
		int selectedDoctorId = ((Doctor) event.getObject()).getId();
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("viewDoctorProfile.xhtml?doctorId=" + selectedDoctorId);
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public void getDoctorListBySpecialty() {		
		if(selectedSpec.equals("All")) {
			resultList = doctorDAO.findAll();
		} else resultList = doctorDAO.selectDoctorListBySpecialtyName(selectedSpec);
	}

	public List<Doctor> getResultList() {	
		return resultList;
	}

	public List<String> getDoctorSpecialtyList() {
		return doctorSpecialtyList;
	}

	public String getSelectedSpec() {
		return selectedSpec;
	}

	public void setSelectedSpec(String selectedSpec) {
		this.selectedSpec = selectedSpec;
	}
	
}